package com.eee.users.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.eee.common.util.PageInfoUtil;
import com.eee.users.dao.IAAdminsDao;
import com.eee.users.pojo.AAdmins;
import com.eee.users.service.IUsersService;

/**
 * service层 一个service持有多个 DAO
 * 
 * 注解： service("usersService") 相当于 bean id = "usersService" class =
 * "com.eee.users.service.impl.UsersServiceImpl"
 * 
 * @author eee
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;

	@Override
	public JSONObject insertOneAdminsSercice(AAdmins admins)
	{
		int res = this.adminsDao.insert(admins);
		return getJSON(res);
	}

	/**
	 * 获取 json对象
	 * 
	 * @param res
	 * @return
	 */
	private JSONObject getJSON(int res)
	{
		JSONObject jsonObject = new JSONObject();
		if (res > 0)
		{
			jsonObject.put("code", "0");
			jsonObject.put("info", "操作成功");
		} else
		{
			jsonObject.put("code", "1");
			jsonObject.put("info", "操作失败");

		}
		return jsonObject;
	}

	/**
	 * 查询一条管理员信息
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		int res = this.adminsDao.delete(admins);
		return getJSON(res);
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		int res = this.adminsDao.update(admins);
		return getJSON(res);
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AAdmins> adminsList = new ArrayList<AAdmins>();

		/**
		 * 处理关键字 keyword 变成 % + keyword + %
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			adminsList = this.adminsDao.findCondList(condMap);
			/* adminsList有一条记录 总记录数 放ID属性上 */
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			/* 每页多少条 */
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			adminsList = this.adminsDao.findCondList(condMap);
		}else
		{
			//不分页查询
			adminsList = this.adminsDao.findCondList(condMap);
		}

		return adminsList;
	}

}
